var express = require('express');
var multiparty = require('multiparty');
var util = require('util');
var fs = require('fs');
var router = express.Router();

var userModel = require('../models/userModel');
var adminModel = require('../models/adminModel');
var checkSession = require('../models/checkSession');

router.get('/', function(req, res, next) {
	var loginbean = checkSession.check(req, res);
	if(loginbean) {
		adminModel.adminIndex(req, res, loginbean);
	}
});

router.get('/publish', function(req, res, next) {
	var loginbean = req.session.loginbean;
	var classify = req.query['classify'];
	var newsId = req.query['newsId'];
	console.log(newsId);
	if(newsId) {
		adminModel.newsUpdate(req, res, loginbean, newsId, classify);
		return;
	}
	res.render('newsPublish', { loginbean: loginbean, classify: classify, news: [], newsId: newsId });
});

router.post('/published', function(req, res, next) {
	var loginbean = checkSession.check(req, res);
	if(loginbean) {
		adminModel.published(req, res, loginbean);
	}
});

router.post('/publishedUpdate', function(req, res, next) {
	var loginbean = req.session.loginbean;
	adminModel.publishedUpdate(req, res, loginbean);
});

router.post('/deleteNews', function(req, res, next) {
	var loginbean = req.session.loginbean;
	adminModel.deleteNews(req, res, loginbean);
});

router.post('/deleteUser', function(req, res, next) {
	var loginbean = req.session.loginbean;
	adminModel.deleteUser(req, res, loginbean);
});

router.post('/deleteUserAdmin', function(req, res, next) {
	var loginbean = req.session.loginbean;
	adminModel.deleteUserAdmin(req, res, loginbean);
});

router.post('/createAdmin', function(req, res, next) {
	var loginbean = req.session.loginbean;
	adminModel.createAdmin(req, res, loginbean);
});

router.post('/addPower', function(req, res, next) {
	var loginbean = req.session.loginbean;
	adminModel.addPower(req, res, loginbean);
});

router.post('/cutPower', function(req, res, next) {
	var loginbean = req.session.loginbean;
	adminModel.cutPower(req, res, loginbean);
});

router.post('/deleteComment', function(req, res, next) {
	var loginbean = req.session.loginbean;
	adminModel.deleteComment(req, res, loginbean);
});

router.post('/changeState', function(req, res, next) {
	var loginbean = req.session.loginbean;
	adminModel.changeState(req, res, loginbean);
});

// 上传图片
router.all('/uploadImg', function(req, res, next) {
	// 由multiparty组件New一个form对象
	var form = new multiparty.Form();
	// 设置编码
	form.encoding = 'utf-8';
	// 设置文件存储路径，因为这个路由是根目录下的路由，所以直接写当前目录下的uploadtemp文件
	form.uploadDir = './uploadtemp/';
	// 设置单文件大小限制
	form.maxFilesSize = 2 * 1024 *1024;
	
	form.parse(req, function (err, fields, files) {
		// 上传完毕之后，调用回调函数
		console.log(files);
		// 通过index获取文件数据
		var file = files['wangEditorH5File'];
		console.log(file);
		// 获取参数名
		var paraname = file[0].fieldName;
		console.log(paraname);
		// 获取原文件名
		var originalFilename = file[0].originalFilename;
		console.log(originalFilename);
		// 获取临时文件的存储路径
		var temPath = file[0].path;
		console.log(temPath);
		// 获取文件大小
		var fileSize = file[0].size;
		console.log(fileSize);
		
		// 构造新存储路径
		var uploadurl = '/img/newsPicture/';
		// 拿到时间戳（字符串类型），拼接文件名，以免上传重名
		var timestamp = new Date().getTime();
		uploadurl += timestamp + originalFilename;
		// 得到新保存路径地址
		var newPath = './public' + uploadurl;
		
		// 准备读写操作
		// 准备读取临时文件路径
		var fileReadStream = fs.createReadStream(temPath);
		// 准备写入正式文件路径
		var fileWriteStream = fs.createWriteStream(newPath);
		// 准备就绪，开启管道流
		fileReadStream.pipe(fileWriteStream);
		// 监听写入事件，一旦写入完毕，执行回调函数
		fileWriteStream.on('close', function () {
			// 删除临时文件夹中的图片
			fs.unlinkSync(temPath);
			console.log('copy over');
			// 根据wangEditor编辑器文档要求，成功后发送保存路径
			res.send(uploadurl);
		});
	});
});


module.exports = router;